using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace BitMango
{
	public static class DateTimeHelper
	{
		public static string GetFriendlyRelativeTime(DateTime dateTime)
		{
			if (DateTime.UtcNow.Ticks == dateTime.Ticks)
			{
				return "Right Now!";
			}
			bool flag = DateTime.UtcNow.Ticks < dateTime.Ticks;
			TimeSpan timeSpan = (DateTime.UtcNow.Ticks >= dateTime.Ticks) ? new TimeSpan(DateTime.UtcNow.Ticks - dateTime.Ticks) : new TimeSpan(dateTime.Ticks - DateTime.UtcNow.Ticks);
			double totalSeconds = timeSpan.TotalSeconds;
			if (totalSeconds < 60.0)
			{
				return (!flag) ? ((timeSpan.Seconds != 1) ? (timeSpan.Seconds + " Seconds Ago") : "One Second Ago") : ("in " + ((timeSpan.Seconds != 1) ? (timeSpan.Seconds + " seconds") : "one second"));
			}
			if (totalSeconds < 120.0)
			{
				return (!flag) ? "A Minute Ago" : "In A Minute";
			}
			if (totalSeconds < 2700.0)
			{
				return (!flag) ? (timeSpan.Minutes + " Minutes Ago") : ("In " + timeSpan.Minutes + " Minutes");
			}
			if (totalSeconds < 5400.0)
			{
				return (!flag) ? "An Hour Ago" : "In An Hour";
			}
			if (totalSeconds < 86400.0)
			{
				return (!flag) ? (timeSpan.Hours + " Hours Ago") : ("In " + timeSpan.Hours + " Hours");
			}
			if (totalSeconds < 172800.0)
			{
				return (!flag) ? "Yesterday" : "Tomorrow";
			}
			if (totalSeconds < 2592000.0)
			{
				return (!flag) ? (timeSpan.Days + " Days Ago") : ("In " + timeSpan.Days + " Days");
			}
			if (totalSeconds < 31104000.0)
			{
				int num = Convert.ToInt32(Math.Floor((double)timeSpan.Days / 30.0));
				return (!flag) ? ((num > 1) ? (num + " Months Ago") : "One Month Ago") : ("In " + ((num > 1) ? (num + " Months") : "One Month"));
			}
			int num2 = Convert.ToInt32(Math.Floor((double)timeSpan.Days / 365.0));
			return (!flag) ? ((num2 > 1) ? (num2 + " Years Ago") : "One Year Ago") : ("In " + ((num2 > 1) ? (num2 + " Years") : "One Year"));
		}

		public static string ToRelativeDate(DateTime input)
		{
			TimeSpan oSpan = DateTime.Now.Subtract(input);
			return DateTimeHelper.ToRelativeDate(oSpan);
		}

		public static string ToRelativeDate(TimeSpan oSpan)
		{
			double TotalMinutes = oSpan.TotalMinutes;
			string str = string.Empty;
			if (TotalMinutes < 0.0)
			{
				TotalMinutes = (double)Mathf.Abs((float)TotalMinutes);
				str = " from Now";
			}
			SortedList<double, Func<string>> sortedList = new SortedList<double, Func<string>>();
			sortedList.Add(0.75, () => "Seconds");
			sortedList.Add(1.5, () => "1 Min");
			sortedList.Add(45.0, () => string.Format("{0} Min", Math.Round(TotalMinutes)));
			sortedList.Add(90.0, () => "1 Hr");
			sortedList.Add(1440.0, () => string.Format("{0} Hrs", Math.Round(Math.Abs(oSpan.TotalHours))));
			sortedList.Add(2880.0, () => "1 Day");
			sortedList.Add(43200.0, () => string.Format("{0} Days", Math.Floor(Math.Abs(oSpan.TotalDays))));
			sortedList.Add(86400.0, () => "a Month");
			sortedList.Add(525600.0, () => string.Format("{0} Months", Math.Floor(Math.Abs(oSpan.TotalDays / 30.0))));
			sortedList.Add(1051200.0, () => "a Year");
			sortedList.Add(double.MaxValue, () => string.Format("{0} Years", Math.Floor(Math.Abs(oSpan.TotalDays / 365.0))));
			return sortedList.First((KeyValuePair<double, Func<string>> n) => TotalMinutes < n.Key).Value() + str;
		}

		public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
		{
			int num = dt.DayOfWeek - startOfWeek;
			if (num < 0)
			{
				num += 7;
			}
			return dt.AddDays((double)(-1 * num)).Date;
		}

		private const int SECOND = 1;

		private const int MINUTE = 60;

		private const int HOUR = 3600;

		private const int DAY = 86400;

		private const int MONTH = 2592000;
	}
}
